<template>
  <div class="user-login">
    <!-- 头部 -->
    <mt-header>
      <router-link to="/WeiPersonal" slot="left">
        <mt-button icon="back"></mt-button>
      </router-link>
      <router-link to="/register" slot="right">
        <mt-button>注册</mt-button>
      </router-link>
    </mt-header>
    <div class="bg">
      <div class="main">
      <!-- 标题文字 -->
      <h2>账号密码登录</h2>
      <!-- 文本框 -->
      <div class="medile">
        <mt-field
          type="tel"
          v-model="uPhone"
          placeholder="请输入账号"
          :state="phoneState"
          @blur.native.capture="checkPhone"
        ></mt-field>
        <mt-field
          type="password"
          placeholder="请输入密码"
          v-model="uPwd"
          :state="pwdState"
          @blur.native.capture="checkPwd"
        ></mt-field>
        
      </div>
      
      <!-- <div class="agreement">
        <em></em>
        <span>我已阅读并同意&nbsp;
            <router-link to="/text" target="_blank">用户协议</router-link>
            &nbsp;和&nbsp;
            <router-link to="/text" target="_blank">隐私政策</router-link>
        </span>
      </div> -->
      <div style="margin: 16px">
        <van-button  class="button-item" 
          round
          block
          type="info"
          native-type="submit"
          @click="checkLogin"
          >立即登录</van-button
        >
      </div>
      <!-- <router-link class="last" to="/login">密码登录</router-link> -->
    </div>
    </div>
    
      <div style="height:70vh"></div>
  </div>
</template>

<style scoped>
.user-login{
  background-color: #f6f6f6;
  min-height: 100vh;
}
/* 背景图 */
.bg{
  background-image: url(/regist/1.png);
  background-repeat: no-repeat;
  background-position-x: center;
  background-position-y: top;
  background-size: 99% 13.5rem;
  background-attachment: initial;
  background-origin: initial;
  background-clip: initial;
  background-color: initial;
 
}
/* 头部 */
.mint-header {
  background-color: #323232;
  color: #ffdd00;
  
}
/* 文本框和按钮 */
.main{
  width: 70vw;
  margin: 0 auto;
  }
  /* h2标题 */
  .main>h2{
    position: relative;
    font-weight: 700;
    font-size: 1.48rem;
    line-height: 0.58rem;
    color: #323232;
    z-index: 2;
    padding: 40px 0 10px 0;
  }
  /* 文本框 */
.main .medile{
  opacity: 0.7;
  border-radius: 10%;
}
/* 同意图标 */
/* .agreement>em{
    display: block;
    width: 0.7rem;
    height: 0.7rem;
    margin-right: 0.1rem;
    background-color: transparent;
    background-image: url(/regist/2.png);
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    position: absolute;
    left: 70px;
    top: 250px;
} */
/* 同意后面的文字 */
/* .agreement>span{
    font-size: 12px;
    line-height: 12px;
    color: #A8A8A8;
    position: absolute;
    left: 85px;
    top: 250px;
} 
.agrennment span a{
  color: #6d6d6d;
}*/
/* button按钮 */
.van-button--info {
    margin-top: 40px;
    font-weight: bold;
    font-size: 1rem;
    line-height: 0.9rem;
    color: #323232;
    background-color: #fd6;
    border: 0px solid #fd6;
}

/* .van-button--disabled {
    color: #CCCCCC;
    background-color: #EEEEEE;
} */
/* 末尾的链接 */
.main .last{
    font-size: 0.28rem;
    color: #323232;
    position: absolute;
    left: 140px;
    top: 375px;
}

</style>

<script>
export default {
  data() {
    return {
      uPhone: "", //用于与用户名双向绑定
      phoneState: "",
      uPwd: "",
      pwdState: "",
    };
  },
  methods: {
    // 滚动条跳转顶部
    top(){
      window.scrollTo(0,0)
    },
    // 验证用户手机号
    checkPhone() {
      let reg = /^1[3-9]\d{9}$/;
      if (reg.test(this.uPhone)) {
        this.phoneState = "success";
        return true;
      } else {
        this.phoneState = "error";
        return false;
      }
    },
    // 验证用户密码
    checkPwd() {
      let reg = /^\d{6}$/;
      if (reg.test(this.uPwd)) {
        this.pwdState = "success";
        return true;
      } else {
        this.pwdState = "error";
        return false;
      }
    },
    // 表单验证
    checkLogin() {
      if (this.checkPhone() && this.checkPwd()) {
        console.log(`发送请求，执行登录。。。`);
        // 发送post请求 发送post请求，传递两个参数：username  password
        this.axios
          .post("/kc/login", `uPhone=${this.uPhone}&uPwd=${this.uPwd}`)
          .then((res) => {
            console.log(res);
            if (res.data.code == 200) {//登录成功
                    this.$toast("登录成功");
                    let {uId,uName,uAvatar} = res.data.result[0]; 
                    sessionStorage.setItem('uId',uId);
                    sessionStorage.setItem('uAvatar',uAvatar);
                    console.log(sessionStorage.uAvatar)
                    sessionStorage.setItem('uName',uName);
                    sessionStorage.setItem('islogin',true);
                    this.$router.push('/');
                    } else{//登录失败
                        this.$toast('账号或密码输入错误');
                    }
          });
      }
    },
  },
  mounted(){
    this.top();
  }
};
</script>